package cn.smbms.tools;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @Auther: liyuxuan
 * @Date:2021/11/8 - 11 - 08 - 11:59
 */
public class ConfigManager {
    private static Properties properties;
    //懒汉模式
    //private static ConfigManager configManager = null;

    //饿汉模式
    private static ConfigManager configManager;
    //饿汉模式
    public static ConfigManager getInstance(){
        configManager = ConfigManagerHelper.INSTANCE;
        return configManager;
    }
    public static class ConfigManagerHelper{
        private static final ConfigManager INSTANCE = new ConfigManager();
    }

    //杜绝外界创建该类对象，私有构造方法
    private ConfigManager(){
        String configFile = "database.properties";
        properties = new Properties();
        InputStream resourceAsStream = ConfigManager.class.getClassLoader().getResourceAsStream(configFile);
        try {
            properties.load(resourceAsStream);
            resourceAsStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //对外提供的获取ConfigManager的实例方法
    /*public static synchronized ConfigManager getInstance(){
        if(configManager==null){
            configManager = new ConfigManager();
        }
        return configManager;
    }*/

    //读取配置文件信息
    public String getValue(String key){
        return properties.getProperty(key);
    }
}
